package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.JurisdictionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/jurisdiction")
public class JurisdictionController {
    /**
     * @program: itcast_health
     * @description:
     * @author: 李先生
     * @create: 2020-07-07 18:24
     **/

    @Reference
    private JurisdictionService jurisdictionService;

    @RequestMapping("/findByPage")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult byPage = jurisdictionService.findByPage(queryPageBean);
        log.debug("byPage:" + byPage);
        return new Result(true, MessageConst.PERMISSION_QUERY_SUCCEEDED, byPage);
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("/add")
    public Result add(@RequestBody Map<String, Object> map, Integer[] checkitemIds) {
        log.debug("checkitemIds:" + Arrays.toString(checkitemIds));
        log.debug("map:" + map);
//      取出数据
        String name = (String) map.get("name");
        String keyword = (String) map.get("keyword");
        String description = (String) map.get("description");  //获取描述信息
        boolean add = jurisdictionService.add(name, keyword, description, checkitemIds);
        if (!add) {
//            返回错误结果
            return new Result(false, MessageConst.PLEASE_MODIFY_THE_NAME_OR_CODE_TO_ADD);
        }
        return new Result(true, MessageConst.NEW_PERMISSIONS_SUCCEEDED);
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("/delById")
    public Result delById(Integer id) {
        log.debug("id:" + id);
//        调用方法执行删除
        jurisdictionService.delById(id);  // 由于是单独的权限不用做判断
        return new Result(true, MessageConst.SUCCESSFULLY_DELETED_PERMISSION);
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        Permission byId = jurisdictionService.findById(id);
        return new Result(true, MessageConst.GET_EXCEPTION, byId);
    }
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("/findAll")
    public Result findAll() {
//        查询所有的角色
        List<Role> roles = jurisdictionService.findAll();
        return new Result(true, MessageConst.QUERY_ALL_ROLES_SUCCESSFULLY, roles);
    }
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("/findByIdRole")
    public Result findByIdRole(Integer id) {
        Map<String, Object> byIdRole = jurisdictionService.findByIdRole(id);
        return new Result(true, "", byIdRole);
    }
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("/edit")
    public Result edit(Integer[] checkitemIds, @RequestBody Permission permission) {
        log.debug("checkitemIds:" + Arrays.toString(checkitemIds));
        log.debug("permission:" + permission);
        jurisdictionService.edit(checkitemIds, permission);
        return new Result(true, MessageConst.PERMISSION_EDITED_SUCCESSFULLY);
    }
}
